package cn.wjdiankong.levideo.douyin.data;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;
import android.util.Log;
import cn.wjdiankong.levideo.data.LevideoData;
import cn.wjdiankong.levideo.utils.Logger;
import cn.wjdiankong.levideo.utils.Utils;

public class DouyinVideoData extends LevideoData{
	
	public static DouyinVideoData fromJSONData(String jsonStr){
		DouyinVideoData data = new DouyinVideoData();
		if(TextUtils.isEmpty(jsonStr)){
			return data;
		}
		try{
			JSONObject json = new JSONObject(jsonStr);
			
			//video标签中的信息
			/**
			 * video
			 * 	-play_addr
			 *  	-url_list
			 *  -download_addr
			 *  	-url_list
			 *  -origin_cover
			 *  	-url_list
			 *  -cover
			 *  	-width
			 *  	-height
			 *  
			 * author
			 * 	-avatar_thumb
			 * 		-url_list
			 * 	-nickname
			 * 
			 * statistics
			 * 	-play_count
			 *  -digg_count
			 * 
			 * music
			 * 	-cover_thumb
			 * 		-url_list
			 * 	-title
			 * 	-author
			 * 
			 * desc
			 * 
			 * create_time
			 */
			
			//视频内容信息
			JSONObject videoJson = json.getJSONObject("video");
			JSONObject playAddJson = videoJson.optJSONObject("play_addr");
			if(playAddJson != null){
				JSONArray jsonAry = playAddJson.optJSONArray("url_list");
				if(jsonAry != null && jsonAry.length() > 0){
					data.videoPlayUrl = jsonAry.getString(0);
				}
				JSONObject downloadJson = videoJson.optJSONObject("download_addr");
				if(downloadJson != null){
					jsonAry = downloadJson.optJSONArray("url_list");
					if(jsonAry != null && jsonAry.length() > 0){
						data.videoDownloadUrl = jsonAry.getString(0);
					}
				}
				JSONObject coverJson = videoJson.optJSONObject("origin_cover");
				if(coverJson != null){
					jsonAry = coverJson.optJSONArray("url_list");
					if(jsonAry != null && jsonAry.length() > 0){
						data.coverImgUrl = jsonAry.getString(0);
					}
				}
				JSONObject sizeJson = videoJson.optJSONObject("cover");
				if(sizeJson != null){
					data.videoWidth = sizeJson.optInt("width");
					data.videoHeight = sizeJson.optInt("height");
				}
			}
			
			data.title = json.optString("desc");
			data.createTime = json.optLong("create_time")*1000;
			
			JSONObject statJson = json.optJSONObject("statistics");
			if(statJson != null){
				data.playCount = statJson.optInt("play_count");
				data.likeCount = statJson.optInt("digg_count");
			}
			
			//视频作者信息
			JSONObject authorJson = json.optJSONObject("author");
			if(authorJson != null){
				JSONObject thumbJson = authorJson.optJSONObject("avatar_thumb");
				if(thumbJson != null){
					JSONArray jsonAry = thumbJson.optJSONArray("url_list");
					if(jsonAry != null && jsonAry.length() > 0){
						data.authorImgUrl = jsonAry.getString(0);
					}
				}
				data.authorName = authorJson.optString("nickname");
				data.authorSignature = authorJson.optString("signature");
				data.authorAge = authorJson.optString("birthday");
				data.authorSex = authorJson.optInt("gender");
			}
			
			//视频音乐信息
			JSONObject musicJson = json.optJSONObject("music");
			if(musicJson != null){
				JSONObject musicThumbJson = musicJson.optJSONObject("cover_thumb");
				if(musicThumbJson != null){
					JSONArray jsonAry = musicThumbJson.optJSONArray("url_list");
					if(jsonAry != null && jsonAry.length() > 0){
						data.musicImgUrl = jsonAry.getString(0);
					}
				}
				data.musicName = musicJson.optString("title");
				data.musicAuthorName = musicJson.optString("author");
			}
			
			data.formatTimeStr = Utils.formatTimeStr(data.createTime);
			data.formatPlayCountStr = Utils.formatNumber(data.playCount);
			data.formatLikeCountStr = Utils.formatNumber(data.likeCount);
			
			if(!TextUtils.isEmpty(data.musicAuthorName) && !TextUtils.isEmpty(data.musicName)){
				if(data.musicName.contains("@")){
					data.filterMusicNameStr = data.musicAuthorName+data.musicName;
				}else{
					data.filterMusicNameStr = data.musicAuthorName+"@"+data.musicName;
				}
			}else if(!TextUtils.isEmpty(data.musicAuthorName)){
				data.filterMusicNameStr = data.musicAuthorName;
			}else if(!TextUtils.isEmpty(data.musicName)){
				data.filterMusicNameStr = data.musicName;
			}
			
			if(!TextUtils.isEmpty(data.title)){
				if(data.title.length() > 30){
					data.filterTitleStr = Utils.filterStrBlank(data.title.substring(0, 30)+"...");
				}else{
					data.filterTitleStr = Utils.filterStrBlank(data.title);
				}
			}
			if(!TextUtils.isEmpty(data.authorName)){
				if(data.authorName.length() > 7){
					data.filterUserNameStr = Utils.filterStrBlank(data.authorName.substring(0, 7)+"...");
				}else{
					data.filterUserNameStr = Utils.filterStrBlank(data.authorName);
				}
			}
		}catch(Exception e){
			Logger.log("parse aweme json err:"+Log.getStackTraceString(e));
		}
		if(TextUtils.isEmpty(data.videoPlayUrl)){
			return null;
		}
		return data;
	}
	
	@Override
	public String toString(){
		return "videotitle="+title+",videoplayurl="+videoPlayUrl+",videodownloadurl="+videoDownloadUrl+",width="+videoWidth+",height="+videoHeight
				+",coverimgurl="+coverImgUrl+",musicname="+musicName+",musicimgurl="+musicImgUrl+",musicauthorname="+musicAuthorName+",authorname="
				+authorName+",authorimgurl="+authorImgUrl+",playcount="+playCount;
	}
	
}
